<template>
  <div class="parking-charge-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>车位收费信息列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="车位号" prop="parkingAreaNumber">
        <el-input v-model="searchParams.parkingAreaNumber" placeholder="请选择车位号" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findParkingArea"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="单元房号" prop="residentRoomNumber">
        <el-input v-model="searchParams.residentRoomNumber" placeholder="请选择单元房号" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findRoom"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="车牌">
        <el-input v-model="searchParams.licensePlate" placeholder="请输入车牌" maxlength="32"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" @click="onExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="parkingAreaNumber" label="车位号"></el-table-column>
      <el-table-column prop="residentRoomNumber" label="单元房号"></el-table-column>
      <el-table-column prop="licensePlate" label="车牌"></el-table-column>
      <el-table-column prop="billingStartDate" label="使用开始时间"></el-table-column>
      <el-table-column prop="billingEndDate" label="使用结束时间"></el-table-column>
      <el-table-column prop="amount" label="金额"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="parkingAreaSelectorVisible" v-if="parkingAreaSelectorVisible" title="车位信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <parking-area-selector @parking-area-selected-event="handleParkingAreaSelectedEvent">
      </parking-area-selector>
    </el-dialog>
    <el-dialog :visible.sync="roomSelectorVisible" v-if="roomSelectorVisible" title="单元信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <room-selector @room-selected-event="handleRoomSelectedEvent">
      </room-selector>
    </el-dialog>
    <el-dialog :visible.sync="parkingChargeAddVisible" v-if="parkingChargeAddVisible" :title="parkingChargeAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <parking-charge-add :id="selectedParkingChargeId" @close-parking-charge-add-event="handleCloseParkingChargeAddEvent" @reset-parking-charge-add-event="handleResetParkingChargeAddEvent">
      </parking-charge-add>
    </el-dialog>
    <el-dialog :visible.sync="parkingChargeViewVisible" v-if="parkingChargeViewVisible" title="车位收费信息详情" :top="dialogTop" :width="dialogWidth">
      <parking-charge-view :id="selectedParkingChargeId" @close-parking-charge-view-event="handleCloseParkingChargeViewEvent">
      </parking-charge-view>
    </el-dialog>
  </div>
</template>

<script>
import parkingChargeApi from '@/api/parkingChargeApi'
import ParkingAreaSelector from "@/views/parkingArea/ParkingAreaSelector.vue";
import RoomSelector from "@/views/room/RoomSelector.vue";
import ParkingChargeAdd from "@/views/parkingCharge/ParkingChargeAdd.vue"
import ParkingChargeView from "@/views/parkingCharge/ParkingChargeView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'

export default {
  name: 'ParkingChargeList',
  components: {ParkingAreaSelector,RoomSelector,ParkingChargeAdd, ParkingChargeView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        parkingAreaId: '',
        parkingAreaNumber: '',
        residentRoomId: '',
        residentRoomNumber: '',
        licensePlate: ''
      },
      parkingAreaSelectorVisible: false,
      roomSelectorVisible: false,
      parkingChargeAddVisible: false,
      parkingChargeAddTitle: '',
      parkingChargeViewVisible: false,
      selectedParkingChargeId: 0
    }
  },
  methods: {
    getPageData (p) {
      return parkingChargeApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        parkingAreaId: '',
        parkingAreaNumber: '',
        residentRoomId: '',
        residentRoomNumber: '',
        licensePlate: ''
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedParkingChargeId = ''
      this.parkingChargeAddVisible = true
      this.parkingChargeAddTitle = '车位收费信息新增'
    },
    onExport () {
      const headers = ['车位号','单元房号','车牌','使用开始时间','使用结束时间','金额','备注']
      const params = Object.assign(this.getPaginationParams(), this.searchParams)
      this.getPageData(params).then(data => {
        if (!data || !data.data || data.data.list.length < 1) {
          this.$message.error('无数据导出')
          return
        }
        const exportData = []
        for (const d of data.data.list) {
          exportData.push([d.parkingAreaNumber,d.residentRoomNumber,d.licensePlate,d.billingStartDate,d.billingEndDate,d.amount,d.remark])
        }
        this.exportToExcel(headers, exportData)
      })
    },
    editRow (id) {
      this.selectedParkingChargeId = id
      this.parkingChargeAddVisible = true
      this.parkingChargeAddTitle = '车位收费信息编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        parkingChargeApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedParkingChargeId = id
      this.parkingChargeViewVisible = true
    },
    findParkingArea () {
      this.parkingAreaSelectorVisible = true
    },
    handleParkingAreaSelectedEvent (selectedParkingArea) {
      this.parkingAreaSelectorVisible = false
      if (selectedParkingArea && 'parkingAreaId' in selectedParkingArea && this.searchParams.parkingAreaId !== selectedParkingArea['parkingAreaId']) {
        this.searchParams.parkingAreaId = selectedParkingArea['parkingAreaId']
        this.searchParams.parkingAreaNumber = selectedParkingArea['parkingAreaNumber']
      }
    },
    findRoom () {
      this.roomSelectorVisible = true
    },
    handleRoomSelectedEvent (selectedRoom) {
      this.roomSelectorVisible = false
      if (selectedRoom && 'residentRoomId' in selectedRoom && this.searchParams.residentRoomId !== selectedRoom['residentRoomId']) {
        this.searchParams.residentRoomId = selectedRoom['residentRoomId']
        this.searchParams.residentRoomNumber = selectedRoom['residentRoomNumber']
      }
    },
    handleCloseParkingChargeViewEvent () {
      this.parkingChargeViewVisible = false
    },
    handleResetParkingChargeAddEvent () {
      this.parkingChargeAddTitle = '车位收费信息新增'
    },
    handleCloseParkingChargeAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.parkingChargeAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>